package org.yunmai.controller;


import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.yunmai.dto.UserPageQuery;
import org.yunmai.entity.SysUser;
import org.yunmai.service.SysUserService;
import org.yunmai.util.R;
import org.yunmai.vo.PageResult;
import org.yunmai.vo.SysUserVO;

import java.util.List;

/**
 * @Author 李孝辉
 * @Date 2025/6/3 21:02
 * @email 1626285873@qq.com
 * @Phone 18724505266
 * @内容
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController {
    @Resource
    SysUserService sysUserService;

    /**
     * 查询所有用户
     * @return
     */
    @GetMapping("/showAll")
    public R<List<SysUser>> showAll() {
        return R.build(200, "查询成功", sysUserService.list());
    }

    @PostMapping("/getById")
    public R<SysUser> getById(Integer id) {
        return R.success(sysUserService.getById(id));
    }

    @DeleteMapping("/deleteById/{id}")
    public R<Boolean> deleteById(@PathVariable Integer id) {
        return R.success(sysUserService.removeById(id));
    }

    @PutMapping("/update/{id}")
    public R<Boolean> update(@RequestBody SysUser sysUser) {
        if (sysUser.getId() == null) {
            return R.error("id不能为空");
        }
        boolean result = sysUserService.updateById(sysUser);
        return R.success(result);
    }
    @PostMapping("/PageList")//用于分页查询user
    public R<PageResult<SysUserVO>> getUserPageList(@RequestBody UserPageQuery query) {
        List<SysUserVO> users = sysUserService.getUserPage(query.getPageNum(), query.getPageSize(), query.getKeyword());
        int total=users.toArray().length;
        PageResult<SysUserVO> result = new PageResult<>();
        result.setList(users);
        result.setTotal(total);
        return R.success(result);
    }

    @PostMapping("/add")
    public R<Boolean> add(@RequestBody SysUser sysUser) {
        boolean result = sysUserService.save(sysUser);
        return R.success(result);
    }
}
